/*
 * @Author: Zhouhai 497675647@qq.com
 * @Date: 2022-08-23 17:15:53
 * @LastEditors: zhouhai zhouhaib@yonyou.com
 * @LastEditTime: 2025-04-10 15:18:04
 */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { GenTableDomain } from '@/type.d/tool';
import type { ProColumns } from '@ant-design/pro-table';
import { ProTable } from '@ant-design/pro-table';
import { message, Modal } from 'antd';
import React, { useState } from 'react'
import { importTable, listDbTable } from '@/api/tool/gen';
import { useStore } from '@/store';

export type ImportTableProps = {
    visible: boolean,
    onOk: (selectkeys: React.Key[]) => void,
    onCancel: () => void,
}



const columns: ProColumns<GenTableDomain>[] = [
    {
        title: '表名称',
        dataIndex: 'tableName',
        copyable: true,
        ellipsis: true,
    },
    {
        title: '表描述',
        dataIndex: 'tableComment',
    },
    {
        title: '创建时间',
        dataIndex: 'createTime',
        search: false,
        valueType: 'dateTime'
    },
    {
        title: '更新时间',
        dataIndex: 'updateTime',
        search: false,
        valueType: 'dateTime'
    },

];

const ImportTable: React.FC<ImportTableProps> = (Props) => {
    const { visible: isModalVisible, onOk: handleOk, onCancel: handleCancel } = Props;
    const { layoutStore } = useStore();

    const [selectTableNames, setSelectTableNames] = useState<string[]>([]);

    const onOK = () => {
        if (!selectTableNames || selectTableNames.length == 0) {
            message.warning("请选择要导入的表");
            return
        }
        layoutStore.OpenSpanning()
        importTable({ tables: selectTableNames.join(',') }).then((res) => {
            handleOk(selectTableNames);
            message.success("导入成功！");
        }).finally(() => {
            layoutStore.CloseSpanning()
        })
    }


    return (
        <>
            <Modal title="导入表" visible={isModalVisible} onOk={onOK} onCancel={handleCancel} width={1000} destroyOnClose>
                <ProTable<GenTableDomain>
                    rowSelection={{
                        type: 'checkbox',
                        onChange: (selectedRowKeys: any) => {
                            setSelectTableNames(selectedRowKeys);
                        }
                    }}
                    columns={columns}
                    cardBordered
                    rowKey="tableId"
                    search={{
                        labelWidth: 'auto',
                    }}
                    options={{
                        setting: {
                            listsHeight: 400,
                        },
                    }}
                    pagination={{
                        pageSize: 5,
                        onChange: (page) => console.log(page),
                    }}
                    dateFormatter="string"
                    request={async (params) => {
                        layoutStore.OpenSpanning()
                        return listDbTable(params).then((res) => {
                            return {
                                data: res.data,
                                total: res.total,
                                success: true,
                            }
                        }).finally(() => {
                            layoutStore.CloseSpanning()
                        });
                    }}
                />
            </Modal>

        </>
    )
}
export default ImportTable;